package com.lineage.server.datatables.sql;

import com.lineage.DatabaseFactory;
import com.lineage.server.datatables.ExtraMagicWeaponTable;
import com.lineage.server.datatables.storage.CharWeaponStorage;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.L1Object;
import com.lineage.server.templates.L1MagicWeapon;
import com.lineage.server.utils.PerformanceTimer;
import com.lineage.server.utils.SQLUtil;
import com.lineage.server.world.World;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class CharWeaponTimeTable implements CharWeaponStorage {
  private static final Log _log = LogFactory.getLog(CharBookTable.class);
  
  public void load() {
    PerformanceTimer timer = new PerformanceTimer();
    Connection cn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    int size = 0;
    try {
      cn = DatabaseFactory.get().getConnection();
      ps = cn.prepareStatement("SELECT * FROM `character_weapon_time`");
      rs = ps.executeQuery();
      Timestamp ts = new Timestamp(System.currentTimeMillis());
      while (rs.next()) {
        int itemr_obj_id = rs.getInt("itemr_obj_id");
        Timestamp usertime = rs.getTimestamp("usertime");
        int magic_weapon = rs.getInt("magic_weapon");
        int steps = rs.getInt("steps");
        if (usertime != null && usertime.before(ts)) {
          delete(itemr_obj_id);
          continue;
        } 
        addValue(itemr_obj_id, usertime, magic_weapon, steps);
        size++;
      } 
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(ps);
      SQLUtil.close(cn);
    } 
    _log.info("读取->武器魔法使用期限资料数量: " + size + "(" + timer.get() + "ms)");
  }
  
  public static int get_steps(int itemobjid) {
    Connection cn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    int thesteps = -1;
    try {
      cn = DatabaseFactory.get().getConnection();
      ps = cn.prepareStatement("SELECT * FROM `character_weapon_time`");
      rs = ps.executeQuery();
      while (rs.next()) {
        int itemr_obj_id = rs.getInt("itemr_obj_id");
        int steps = rs.getInt("steps");
        if (itemr_obj_id == itemobjid) {
          thesteps = steps;
          break;
        } 
      } 
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(ps);
      SQLUtil.close(cn);
    } 
    return thesteps;
  }
  
  private static void addValue(int itemr_obj_id, Timestamp usertime, int magic_weapon, int steps) {
    L1Object obj = World.get().findObject(itemr_obj_id);
    boolean isError = true;
    if (obj != null && obj instanceof L1ItemInstance) {
      L1ItemInstance item = (L1ItemInstance)obj;
      item.set_time(usertime);
      if (magic_weapon > 0) {
        L1MagicWeapon magicWeapon = ExtraMagicWeaponTable.getInstance().get(magic_weapon, steps);
        item.set_magic_weapon(magicWeapon);
      } 
      isError = false;
    } 
    if (isError)
      delete(itemr_obj_id); 
  }
  
  public static void delete(int objid) {
    Connection cn = null;
    PreparedStatement ps = null;
    try {
      cn = DatabaseFactory.get().getConnection();
      ps = cn.prepareStatement("DELETE FROM `character_weapon_time` WHERE `itemr_obj_id`=?");
      ps.setInt(1, objid);
      ps.execute();
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(ps);
      SQLUtil.close(cn);
    } 
  }
  
  public void addTime(int itemr_obj_id, Timestamp usertime, int magic_weapon) {
    Connection co = null;
    PreparedStatement ps = null;
    try {
      co = DatabaseFactory.get().getConnection();
      ps = co.prepareStatement(
          "INSERT INTO `character_weapon_time` SET `itemr_obj_id`=?,`usertime`=?,`magic_weapon`=?,`steps`=?");
      int i = 0;
      ps.setInt(++i, itemr_obj_id);
      ps.setTimestamp(++i, usertime);
      ps.setInt(++i, magic_weapon);
      ps.setInt(++i, 0);
      ps.execute();
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(ps);
      SQLUtil.close(co);
    } 
  }
  
  public static void addTimeUP(int itemr_obj_id, Timestamp usertime, int magic_weapon, int steps) {
    Connection co = null;
    PreparedStatement ps = null;
    try {
      co = DatabaseFactory.get().getConnection();
      ps = co.prepareStatement(
          "INSERT INTO `character_weapon_time` SET `itemr_obj_id`=?,`usertime`=?,`magic_weapon`=?,`steps`=?");
      int i = 0;
      ps.setInt(++i, itemr_obj_id);
      ps.setTimestamp(++i, usertime);
      ps.setInt(++i, magic_weapon);
      ps.setInt(++i, steps);
      ps.execute();
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(ps);
      SQLUtil.close(co);
    } 
  }
  
  public void updateTime(int itemr_obj_id, Timestamp usertime, int magic_weapon, int steps, int same) {
    Connection co = null;
    PreparedStatement ps = null;
    try {
      co = DatabaseFactory.get().getConnection();
      ps = co.prepareStatement(
          "UPDATE `character_weapon_time` SET `usertime`=?,`magic_weapon`=?,`steps`=? WHERE `itemr_obj_id`=?");
      int i = 0;
      ps.setTimestamp(++i, usertime);
      ps.setInt(++i, magic_weapon);
      if (same == 0)
        ps.setInt(++i, 0); 
      if (same == 1)
        ps.setInt(++i, steps); 
      ps.setInt(++i, itemr_obj_id);
      ps.execute();
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(ps);
      SQLUtil.close(co);
    } 
  }
}
